package com.hqyj.smvc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.smvc.mapper.CompanyMapper;
import com.hqyj.smvc.mapper.EmployeeMapper;
import com.hqyj.smvc.mapper.IdcardMapper;
import com.hqyj.smvc.pojo.Employee;
import com.hqyj.smvc.pojo.Idcard;
import com.hqyj.smvc.service.CompanyService;
import com.hqyj.smvc.service.EmployeeService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    @Qualifier("employeeMapper")
    private EmployeeMapper employeeMapper;
    @Autowired
    @Qualifier("idcardMapper")
    private IdcardMapper idcardMapper;
    @Autowired
    @Qualifier("companyMapper")
    private CompanyMapper companyMapper;

    //登录
    @Override
    public Employee login(Employee employee){
        Employee employee1 = employeeMapper.selectEmployee(employee);
        return employee1;
    }

    //展示所有用户
    @Override
    public List<Employee> ShowTheUser(int i){
        Page<Employee> employeePage = PageHelper.startPage(i, 3);
        List<Employee> employees = employeeMapper.selectEmployeeList();

        return employees;
    }

    //查询idcard_id为null值的雇员
    @Override
    public List<Employee> noIdcardEmployee(){
        List<Employee> employees = employeeMapper.selectNoIdcard();
        return employees;
    }

    //查询company_id相同的雇员
    @Override
    public List<Employee> companyEmployee(int id){
        List<Employee> employeeList = employeeMapper.selectCompanyEmployee(id);
        return employeeList;
    }

    //删除雇员
    @Override
    public void fireEmployees(int id){
        Employee employee = employeeMapper.selectIdcardId(id);
        Integer idcard_id = employee.getIdcard_id();
        System.out.println(idcard_id);
        int i = employeeMapper.deleteEmployeeIdcard(id);
        if(idcard_id != null) {
            idcardMapper.deleteEmployeeIdIdcard(idcard_id);
        }
    }

    //按照id查找雇员其他信息
    @Override
    public Employee employeeInformation(int id){
        Employee employee = employeeMapper.selectInformation(id);
        return employee;
    }

    //编辑雇员信息
    @Override
    public void editEmployeeInformation(Employee employee){
        int employee1 = employeeMapper.updateEmployee(employee);
    }

    // 添加雇员
    @Override
    public void appendEmployee(Employee employee){
        Integer company_id = employee.getCompany().getCompany_id();
        Integer idcard_id = employee.getIdcard().getIdcard_id();
        // 判断是添加新公司还是选择已有部门
        String company_name = employee.getCompany().getCompany_name();
        String company_address = employee.getCompany().getCompany_address();
        if (company_name != null && company_address != null) {
            int company = companyMapper.insertCompany(employee.getCompany());
            if (company > 0) {
                company_id = employee.getCompany().getCompany_id();
            }
        }
        // 判断是否需要添加身份证
        String idcard_num = employee.getIdcard().getIdcard_num();
        String idcard_starttime = employee.getIdcard().getIdcard_starttime();
        String idcard_endtime = employee.getIdcard().getIdcard_endtime();
        if(idcard_num != null && (idcard_starttime != null && idcard_endtime != null)) {
            int idcard = idcardMapper.insertIdcard(employee.getIdcard());
            if (idcard > 0){
                idcard_id = employee.getIdcard().getIdcard_id();
            }
        }
        // 将添加生成的主键值封装到雇员对象
        employee.setIdcard_id(idcard_id);
        employee.setCompany_id(company_id);
        // 添加雇员
        employeeMapper.insertEmployee(employee);
    }

}
